import lxml
import requests
from lxml.etree import HTML

page = 0
url = 'https://list.iqiyi.com/www/1/----------0---11-{}-1-iqiyi--.html'

def get_data(url):
    s = requests.session()
    r = s.get(url,
              headers={
                  'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
              })
    r.encoding = 'utf-8'
    r.raise_for_status()
    html = HTML(r.text)
    # jobs1 = html.xpath("//div[@class='qy-list-wrap']")
    # assert isinstance(jobs1[0], lxml.etree._Element)
    # print(jobs1[0].text)
    jobs = html.xpath('//li[@class = "qy-mod-li"]/div/div[2]')
    return jobs
i = 0
while True:
    with open('./movies.txt', 'wb') as m:
        page += 1
        jobs = get_data(url.format(page))
        jobs_1 = get_data(url.format(page-1))
        movies = {}
        for job in jobs:
            # print(job)
            # print(jobs_1)
            if job in jobs_1:
                break
            i += 1
            movies['id'] = i
            movies['name'] = job.xpath('./p[1]/a')[0].text
            movies['score'] = job.xpath('./p[1]/span')[0].text
            movies['address'] = 'https:' + str(job.xpath('./p[1]/a/@href')[0])
            movies['performers'] = job.xpath('./p[2]')[0].text.replace('主演:', '')
            print(movies)


